//
//  MailListTableViewCell.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-2-7.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "MailListTableViewCell.h"
#import "UIView+Addtions.h"
#import "NSString+Addtions.h"

#define MailAccessoryWidth 10

@interface MailListTableViewCell ()

@property (nonatomic, strong) UIImageView *accessoryImage;
@property (nonatomic, strong) UIButton *signBtn;
@property (nonatomic, strong) UIButton *deleteBtn;

@property (nonatomic, strong) UIView *buttonsView;
@property (nonatomic, strong) UIView *containerView;

@property (nonatomic, assign) BOOL isMoving;
@end

@implementation MailListTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.width = AppWidth;
        [self initButtonView];
        self.fromPerson = [[UILabel alloc] initWithFrame:CGRectZero];
        self.fromPerson.backgroundColor=[UIColor clearColor];
        [_containerView addSubview:_fromPerson];

        self.title = [[UILabel alloc] initWithFrame:CGRectZero];
        self.title.backgroundColor=[UIColor clearColor];
        [_containerView addSubview:_title];

        self.fromDate = [[UILabel alloc] initWithFrame:CGRectZero];
        self.fromDate.backgroundColor=[UIColor clearColor];
        [_containerView addSubview:_fromDate];

//        self.description = [[UILabel alloc] initWithFrame:CGRectZero];
//        [self addSubview:_description];

        self.isReadImage = [UIImageView.alloc initWithImage:[UIImage imageNamed:@"img_tips"]];
        [_isReadImage setHighlightedImage:[UIImage imageNamed:@"selectedCell"]];
        [_containerView addSubview:_isReadImage];
        
//        self.attachNum = [[UILabel alloc] initWithFrame:CGRectZero];
//        _attachNum.hidden = YES;
        self.hasAttach = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mail_attach"]];
        _hasAttach.hidden = YES;

//        self.accessoryImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory"]];
//        [self addSubview:_accessoryImage];
        [self setDeafultValue];
        
        UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeGesture:)];
        leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
        [self addGestureRecognizer:leftSwipeGesture];
        
        UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeGesture:)];
        rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
        [self addGestureRecognizer:rightSwipeGesture];
    }
    return self;
}

- (void)initButtonView {
    
    self.buttonsView = [[UIView alloc] initWithFrame:CGRectMake(self.width - ButtonWidth*2, 0, ButtonWidth*2, SwipeEnableTableCellHeight)];
    [self addSubview:_buttonsView];
    //    _buttonsView.backgroundColor = [UIColor yellowColor];
    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, SwipeEnableTableCellHeight)];
    _containerView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_containerView];
    [self addSignBtn];
    [self addDeleteBtn];
    [self setButtonsHidden:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    
    [_isReadImage setHighlighted:highlighted];
    if (highlighted) {
        _fromPerson.textColor = [UIColor whiteColor];
        _title.textColor = [UIColor whiteColor];
//        _description.textColor = [UIColor whiteColor];
        _fromDate.textColor = [UIColor whiteColor];
    } else {
        _fromPerson.textColor = [UIColor grayColor];
        _title.textColor = [UIColor blackColor];
//        _description.textColor = [UIColor grayColor];
        _fromDate.textColor = [UIColor grayColor];
    }
    if (!_isReadImage.hidden) {
        _title.textColor = [UIColor redColor];
    }
}

- (void)setDeafultValue {
    _fromPerson.font = [UIFont systemFontOfSize:15];
    _fromPerson.textColor = [UIColor grayColor];
    
    _title.font = [UIFont systemFontOfSize:16];
    _title.numberOfLines = 0;

//    _description.font = [UIFont systemFontOfSize:16];
//    _description.textColor = [UIColor grayColor];
//    _description.numberOfLines = 2;
    
    _fromDate.font = [UIFont systemFontOfSize:14];
    _fromDate.textColor = [UIColor grayColor];
    
//    _attachNum.font = [UIFont systemFontOfSize:14];
//    _attachNum.backgroundColor = [UIColor grayColor];
//    _attachNum.textColor = [UIColor whiteColor];
//    _attachNum.layer.cornerRadius = 3;
//    _attachNum.textAlignment = NSTextAlignmentCenter;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _title.left = isReadWidth;
    _title.width = AppWidth - isReadWidth - MailAccessoryWidth;
    _title.top = 10;
    
    // TODO: ADD SOME CLCA
    CGFloat singleTextHeight = [@"123" getTextHeightWithFont:_title.font withSuperViewWidth:_title.width];
    CGFloat titleHeight = [_title.text getTextHeightWithFont:_title.font withSuperViewWidth:_title.width];
    CGFloat realHeight = titleHeight > singleTextHeight ? singleTextHeight*2 : singleTextHeight;
    _title.height = realHeight;
    
    [_fromDate sizeToFit];
    _fromDate.top = 13 + singleTextHeight*2;
    NSLog(@"AppWidth=%fMailAccessoryWidth=%d_fromDate.width=%f",AppWidth,MailAccessoryWidth,_fromDate.width);
    _fromDate.left = AppWidth- MailAccessoryWidth - _fromDate.width;
    
    if (XAppDelegate.isFullScreen) {
        _fromDate.left = AppWidth-PAD_LEFT_NAV_WIDTH_CONST- MailAccessoryWidth - _fromDate.width;
    }

    [_fromPerson sizeToFit];
    _fromPerson.top = 13 + singleTextHeight*2;
    _fromPerson.left = isReadWidth;
    _fromPerson.width = AppWidth - isReadWidth - _fromDate.width - MailAccessoryWidth;

    _isReadImage.width = isReadWidth*4/8;
    _isReadImage.height = isReadWidth*4/8;
    _isReadImage.centerX = isReadWidth*0.5;
    _isReadImage.centerY = 10+singleTextHeight*0.5;

    if (_hasAttach.hidden == NO) {
        [_containerView addSubview:_hasAttach];
        _hasAttach.width = _hasAttach.height = isReadWidth*0.7;
        _hasAttach.centerX = isReadWidth*0.5;
        _hasAttach.centerY = 10+singleTextHeight*1.5;
        _hasAttach.alpha = 1.0f;
    }

//    _accessoryImage.width = MailAccessoryWidth*0.4;
//    _accessoryImage.height = MailAccessoryWidth*0.4*26/16;
//    _accessoryImage.centerY = 10+singleTextHeight*0.5;
//    _accessoryImage.centerX = _title.right + MailAccessoryWidth*0.25;

    if (!ISIOS7) {
        if (self.editingStyle == UITableViewCellEditingStyleDelete) {
            if (self.showingDeleteConfirmation == YES) {
                CGFloat changeX = 60 - MailAccessoryWidth;
                _fromPerson.width -= changeX;
                _fromDate.left -= changeX;
                _title.width -= changeX;
                _hasAttach.alpha = 0.0f;
            }
        }
    }
}

- (void)prepareForReuse {
  
    self.containerView.left = 0;
    self.accessoryType = UITableViewCellAccessoryNone;
    [self setButtonsHidden:YES];
    [super prepareForReuse];
}


- (void)addSignBtn {
    
    self.signBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _signBtn.frame = CGRectMake(0, 0, ButtonWidth, SwipeEnableTableCellHeight);
    _signBtn.backgroundColor = DownloadButtonBGColor;
    [_signBtn setTitle:@"签收" forState:UIControlStateNormal];
    [_signBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_signBtn addTarget:self action:@selector(signBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    [_buttonsView addSubview:_signBtn];
}

- (void)signBtnPressed {
    NSLog(@"Sign ...");
    if (_delegate && [_delegate respondsToSelector:@selector(cellSignButtonPressed:)]) {
        [_delegate cellSignButtonPressed:self];
    }
}

- (void)addDeleteBtn {
    
    self.deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteBtn.frame = CGRectMake(ButtonWidth, 0, ButtonWidth, SwipeEnableTableCellHeight);
    _deleteBtn.backgroundColor = DeleteButtonBGColor;
    [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [_deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_deleteBtn addTarget:self action:@selector(deleteBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    [_buttonsView addSubview:_deleteBtn];
}

- (void)deleteBtnPressed {
    NSLog(@"delete ...");
    if (_delegate && [_delegate respondsToSelector:@selector(cellDeleteButtonPressed:)]) {
        [_delegate cellDeleteButtonPressed:self];
    }
}

#pragma mark - swip gesture

- (void)leftSwipeGesture:(UISwipeGestureRecognizer *)swipe {
    NSLog(@"LEFT ...ss");
    if ( _isMoving) {
        return;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(cellMoveLeftStarted:)]) {
        [_delegate cellMoveLeftStarted:self];
    }
}

- (void)moveLeft {
    CGFloat moveValue = _buttonsView.width;
    NSLog(@"moveValue:%f", moveValue);
    [self setButtonsHidden:NO];
    
    [UIView animateWithDuration:0.35f animations:^{
        _containerView.left = -moveValue;
    } completion:^(BOOL finished) {
        _isMoving = NO;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        if (_delegate && [_delegate respondsToSelector:@selector(cellMoveLeftEnded:)]) {
            [_delegate cellMoveLeftEnded:self];
        }
    }];
}

- (void)rightSwipeGesture:(UISwipeGestureRecognizer *)swipe {
    NSLog(@"RIGHT ...");
    if ( _isMoving) {
        return;
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(cellMoveRightStarted:)]) {
        [_delegate cellMoveRightStarted:self];
    }
}

- (void)moveRight {
    
    [UIView animateWithDuration:0.35f animations:^{
        _containerView.left = 0;
    } completion:^(BOOL finished) {
        _isMoving = NO;
        self.selectionStyle = ISIOS7?UITableViewCellSelectionStyleDefault:UITableViewCellSelectionStyleBlue;
        [self setButtonsHidden:YES];
        if (_delegate && [_delegate respondsToSelector:@selector(cellMoveRightEnded:)]) {
            [_delegate cellMoveRightEnded:self];
        }
    }];
}
- (void)setButtonsHidden:(BOOL)animated {
    self.signBtn.hidden = animated;
    self.deleteBtn.hidden = animated;
}


@end
